package com.logistics.rcs.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.logistics.rcs.entity.domain.RcsCoordinate;
import com.logistics.rcs.entity.model.RcsCoordinateRow;
import com.logistics.rcs.mapper.RcsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@DS("rcs")
public class RcsService {

    @Autowired
    private RcsMapper rcsMapper;

    /**
     * 根据点位查找坐标信息
     * @param points
     * @return
     */
    public List<RcsCoordinateRow> coordinateRows(List<String> points){
        Map<String, Object> params = new HashMap<>();
        params.put("points", points);
        return rcsMapper.findCoordinateByPoints(params).stream().
                map(RcsCoordinateRow::build).collect(Collectors.toList());
    }

    /**
     * 查找坐标信息
     * @param point
     * @return
     */
    public RcsCoordinateRow coordinateRow(String point){
        Map<String, Object> params = new HashMap<>();
        params.put("point", point);
        List<RcsCoordinate> result = rcsMapper.findCoordinateByPoints(params);
        if(result.isEmpty()){
            return null;
        }
        return RcsCoordinateRow.build(result.get(0));
    }

    /**
     * 所有点位坐标
     * @return
     */
    public Map<String, RcsCoordinateRow> allCoordinateRows(){
        return rcsMapper.findAll().stream().
                collect(Collectors.toMap(RcsCoordinate::getPoint, RcsCoordinateRow::build));
    }
}
